<?php
/**
 * @author 	 	 supfire@sohu.com
 * @copyright    	 sns
 * @version      	 1.0
 * @name 		 pic.php
 * @todo 		 照片action
 * @final 		 2009-05-25
 */

$userid = $user->userinfo['userid'];
!$iid && exit('iid 参数不对');
$roles = $user->userinfo['roles'];

$type	= isset($type) ? $type : 4;//1:pic评论, 2:blog日志评论, 3: 个人留言, 4:
$actionComment	= isset($actionComment) ? $actionComment : 'list';//待定参数
$cls	= isset($cls) ? $cls : 0;// 0 : 需不需要回复 , 1:需要回复

$actionPost	= isset($_POST['actionPost']) ? $_POST['actionPost'] : "";

//删除处理
$actiondel	= isset($_GET['actiondel']) ? $_GET['actiondel'] : '';

if($actiondel == 'delComment') //删除单条留言
{
    $commentid	= isset($_GET['commentid']) ? $_GET['commentid'] : 0;
    deleteComOne($iid, $commentid, $type, $userid, $roles);
    $request_uri = $_SERVER[REQUEST_URI];
    $request_uri = str_replace('&actiondel=delComment','',$request_uri); 
    //func::alertMsg('删除成功', $request_uri);
}elseif($actiondel == 'delAllComment') //删除所有留言
{
    delAllComment($iid, $type, $userid);
    $request_uri = $_SERVER[REQUEST_URI];
    $request_uri = str_replace('&actiondel=delAllComment','',$request_uri); 
   // func::alertMsg('删除成功', $request_uri);
}
switch($type)
{
case 4:
    $touid = $iid;

    $typename1 = '给 ';
    $typename = $rsuser[0][username];
    break; 
case 5:
    $touid = db::getOne(DB_TABLEPRE . 'web_doneeinfo', 'userid', 'doneeid = '.$iid);
//SELECT COUNT(doneeid) AS num FROM sns_web_doneeinfo WHERE 1 AND doneeclass like '%%' AND doneename like '%%' limit 1
    $typename1 = '在捐赠 ';
    $typename = $don_oneinfo[doneename];
    break; 
case 6:
    $touid = db::getOne(DB_TABLEPRE . 'web_sharefile', 'userid', 'id = '.$iid);
//SELECT COUNT(a.) AS num FROM sns_ a LEFT JOIN sns_user_basic b USING (userid) WHERE 1 AND a.brief like '%%' AND a.filetitle like '%%' limit 1
    $typename = '共享';
    break; 
case 7:
    $touid = db::getOne(DB_TABLEPRE . 'web_activity', 'userid', 'id = '.$iid);
//SELECT COUNT(id) AS num FROM sns_web_activity WHERE 1 AND actorg like '%%' AND actname like '%%' limit 1
    $typename1 = '在活动 ';
    $typename = $arr_activitys['actname'];
    break; 
}

//处理操作
switch ($actionComment)
{
case 'list':
    //$iid, $roleuserid, $userid, $comment, $type, $parentid
    !$roleuserid && $roleuserid = 0;
    if($actionPost == 'add') //评论及回复
    {
	$comment	= isset($_POST['comment']) ? $_POST['comment'] : '';
        $parentid	= isset($_POST['parentid']) ? $_POST['parentid'] : 0;
     
        $message = ''.$typename1.' 
            “<a href="http://'.YCTOOL_HTTPHOST .'/'. $comenttheurl .'">'.$typename . '</a>” 留了言 ' ;

        //$title = ''.$typename1.' 
	//	＂'.$typename . '＂ 留了言 ' ; 
        $title = $message;
        db::insertRs(DB_TABLEPRE . 'msg_action','userid, 
                            friendid, type, message, addtime, note', "$userid, $touid, 0, '$message', ". time() .", ''"); 

        if($touid != $userid)
        {
            //$touid = $touid . ',';
            include_once(DIR_MODEL . 'msg.func.php');
            msg::sendMessage($touid, $userid, $title, $message, 1);
        }

	addComent($iid, $roleuserid, $userid, $comment, $type, $parentid);
    }

    $page = intval($_GET["page"]);
    if ($page < 1)
    {
	$page = 1;
    }
    $comentcount = getComentCount($iid, $type);
    $rs = getComentAll($iid, $type, $page, $per);
    if($cls == 0)
    {
	$recoment[0] = $rs;
    }elseif($cls == 1)
    {
	foreach ($rs as $i => $value)
	{
	   if(!$value['parentid'])
	    {
		$recoment[0][] = $value;
	    }
	   else
	   {
	       $recoment[1][] = $value;
	   }
	}
    }
    unset($rs);
    break; 
default:;
}

/**
 * 留言总数量 
 * @param int $iid
 * @return array $rs
 * @author supfire
 * @final 2008-09-18
 */
function getComentCount($iid, $type)
{
    return db::getCount(DB_TABLEPRE . 'web_comment', 'commentid', 
	'iid = '.$iid.' AND parentid = 0 AND type = '.$type);		
}

/**
 * 添加留言
 * @param string $iid, $userid, $comment, $type, $parentid
 *                   $type 1 为图片  2位日志   
 * @return array bool
 * @author supfire
 * @final 2008-09-18
 */
function addComent($iid, $roleuserid, $userid, $comment, $type, $parentid = 0)
{
    return db::insertRs(DB_TABLEPRE . 'web_comment', 'iid, roleuserid, userid, parentid, comment,
	type, addtime', "$iid, $roleuserid, $userid, $parentid, '$comment', $type, ".time());
}

/**
 * 通过 iid 获取该 类下 所有留言的信息
 * @return array $rs
 * @author supfire
 * @final 2008-09-18
 */
function getComentAll($iid, $type, $startpage, $per)
{
    return db::getLeftJoin(DB_TABLEPRE . 'web_comment', DB_TABLEPRE . 'user_basic', 
	 'a.commentid, a.iid, a.userid, a.parentid, a.comment,
	  b.face, b.username, a.type, a.addtime, a.roleuserid', 'a.userid = b.userid', 
	  'iid = ' . $iid . ' AND type = '.$type.'
	  ORDER BY a.commentid DESC', $startpage, $per);		
}

/**
 * 通过 userid cid 删除留言/回复
 * @author supfire
 * @final 2008-08-24
 */
function deleteComOne($iid, $commentid, $type, $userid, $roles)
{

    if($roles == 4)
    {
	return db::deleteRs(DB_TABLEPRE . 'web_comment', 
	'(commentid = '.$commentid.' OR parentid = '.$commentid.') 
	 AND iid = '.$iid.' AND type = '.$type);
    }
    else
    {
	return db::deleteRs(DB_TABLEPRE . 'web_comment', 
	'(commentid = '.$commentid.' OR parentid = '.$commentid.') 
	AND (userid = '.$userid.' OR roleuserid = '.$userid.') AND iid = '.$iid.' AND type = '.$type);
    }
}

/**
 * 通过 userid cid 删除 该类下所有的 留言/回复
 * @author supfire
 * @final 2008-08-24
 */
function delAllComment($iid, $type, $userid)
{
    return db::deleteRs(DB_TABLEPRE . 'web_comment', 'iid = '.$iid.' 
	AND userid = '.$userid.' AND type = '.$type);
}
?>
